package com.hth.zhushanmall.product.web;

import com.hth.zhushanmall.product.entity.CategoryEntity;
import com.hth.zhushanmall.product.service.CategoryService;
import com.hth.zhushanmall.product.vo.Catelog2Vo;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author hutianhui
 * @Date 2020/6/16
 **/
@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    /*@Resource
    RedissonClient redisson;

    @Autowired
    StringRedisTemplate redisTemplate;*/

    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){

        //TODO 1.查出所有的1级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Categorys();

        //试图解析器进行拼串
        model.addAttribute("categorys",categoryEntities);
        return "index";
    }

    //index/catalog.json
    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catelog2Vo>> getCatelogJson(){

        Map<String, List<Catelog2Vo>> catelogJson = categoryService.getCatelogJson();

        return catelogJson;

        //return categoryService.getCatelogJson();
    }

/*    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        //1.获取一把锁，只要锁的名字一样，就是同一把锁
        RLock lock = redisson.getLock("my-lock");

        //2.加锁
        //lock.lock();阻塞式等待，默认加的锁都是30s时间。
        //锁的自动续期，如果业务超长，运行期间自动给锁续上新的30s。不用担心业务时间长，锁自动过期被删除
        //加锁的业务子要运行完成，就不会给当前锁续期，即使不手动解锁，锁默认在30s以后自动删除。
        lock.lock(10, TimeUnit.SECONDS);//10秒自动解锁

        try {
            System.out.println("加锁成功，执行业务..."+Thread.currentThread().getId());
            Thread.sleep(30000);
        } catch (Exception e){

        } finally {
            //3.解锁  将设解锁代码没有运行，redisson会不会出现死锁
            System.out.println("释放锁..."+Thread.currentThread().getId());
            lock.unlock();
        }
        return "hello";
    }

    //保证一定能读到最新数据，修改期间，写锁是一个排他锁（互斥锁、独享锁）。读锁是一个共享锁
    //写锁没释放读就必须等待
    //读+ 读：相当于无锁，并发锁，只会在redis中记录好，所有当前锁。他们都会同时加锁成功
    //写 + 读：等待写锁释放
    //写 + 读：阻塞方式
    //读 + 写：有读锁。写也需要等待。
    //只要有写的存在，都必须等待
    @GetMapping("/write")
    @ResponseBody
    public String writeValue(){

        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        String s = "";
        RLock rLock = lock.writeLock();
        try {
            //1.该数据加写锁，读数据加读锁
            rLock.lock();
            s = UUID.randomUUID().toString();
            Thread.sleep(30000);
            redisTemplate.opsForValue().set("writeValue",s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return s;
    }

    @GetMapping("/read")
    @ResponseBody
    public String readValue(){
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        String s = "";
        RLock rLock = lock.readLock();
        try {
            //
            s = redisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;
    }*/


}
